<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="box">111</div>
    <div class="box">222</div>
    <div class="box">333</div>
    <div class="box">444</div>
    <div class="box">555</div>
    <script>
        var box = document.getElementsByClassName("box");
        // for (var i = 0; i < box.length; i++) {

        //     (function(i) {
        //         box[i].addEventListener("click", function() {
        //             alert(i)
        //         })
        //     })(i)

        // }

        for (let i = 0; i < box.length; i++) {

            box[i].addEventListener("click", function() {
                alert(i)
            })

        }
    </script>
</body>

</html>